package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.OrderSettingListService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSettingList")
public class OrderSettingListController {
    @Reference
    private OrderSettingListService ordersettingListService;

    /**
     * 预约列表分页查询
     * @return
     */
    @RequestMapping("/findOrdersPage")
    public PageResult findOrdersPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = ordersettingListService.findOrdersPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据手机号和预约日期与姓名添加预约信息
     * @param
     * @param setmealId
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Order order, Integer setmealId){
        try {
            ordersettingListService.add(order,setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败
            return new Result(false, MessageConstant.ADD_ORDER_FAIT);
        }
        //添加成功
        return new Result(true,MessageConstant.ADD_ORDER_SUCCESS);
    }

    /**
     * 点击编辑按钮，根据id查询预约信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map = ordersettingListService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    /**
     * 根据预约id查询套餐id
     * @return
     */
    @RequestMapping("/findSetmealIdByOrderId")
    public Result findSetmealIdByOrderId(Integer id){
        try {
            Integer setmealId = ordersettingListService.findSetmealIdByOrderId(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 编辑
     * @param setmealId
     * @param
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Integer setmealId,@RequestBody Order order){
        try {
            ordersettingListService.edit(setmealId,order);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑预约信息失败
            return new Result(false,MessageConstant.EDIT_ORDER_FAIT);
        }
        return new Result(true,MessageConstant.EDIT_ORDER_SUCCESS);
    }

    /**
     * 根据id删除预约信息
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            ordersettingListService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIT);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }

    /**
     * 预约状态显示
     * @param
     * @param order
     * @return
     */
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestBody Order order){
        try {
            ordersettingListService.changeStatus(order);
        } catch (Exception e) {
            e.printStackTrace();
            //改变预约状态失败
            return new Result(false, MessageConstant.UPDATE_ORDERSTATUS_FAIL);
        }
        //改变预约状态成功
        return new Result(true, MessageConstant.UPDATE_ORDERSTATUS_SUCCESS);
    }
}
